package com.kaibes.platform.article;

import java.io.IOException;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.ObjectCodec;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.kaibes.platform.article.enums.ArticleStatus;
import com.kaibes.platform.article.enums.ArticleType;
import com.kaibes.platform.article.pojo.ArticleUpdate;

public class ArticleUpdateDeserialize extends JsonDeserializer<ArticleUpdate> {

	@Override
	public ArticleUpdate deserialize(JsonParser p, DeserializationContext ctxt)
			throws IOException, JsonProcessingException {
		ObjectCodec oc = p.getCodec();
        JsonNode node = oc.readTree(p);
        ArticleUpdate update = new ArticleUpdate();
        update.setCategoryId(null);
        update.setTitle(node.get("title").asText());
        update.setContent(node.get("content").asText());
        update.setCategoryId((short) node.get("categoryId").asInt());
        update.setType(ArticleType.valueOf(node.get("type").asText()));
        update.setTags(node.get("tags").asText());
        update.setThumbnail(node.get("thumbnail").asText());
        update.setTop(node.get("top").asBoolean());
        update.setCommentAble(node.get("commentAble").asBoolean());
        update.setStatus(ArticleStatus.valueOf(node.get("status").asText()));
		return update;
	}

}
